package com.zzy.coupon.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.zzy.coupon.entity.MemberPriceEntity;
import com.zzy.coupon.entity.SkuLadderEntity;
import com.zzy.coupon.service.MemberPriceService;
import com.zzy.coupon.service.SkuLadderService;
import com.zzy.utils.to.MemberPrice;
import com.zzy.utils.to.SkuReductionTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.Query;

import com.zzy.coupon.mapper.SkuFullReductionMapper;
import com.zzy.coupon.entity.SkuFullReductionEntity;
import com.zzy.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReductionEntity> implements SkuFullReductionService {

	@Autowired
	private SkuLadderService skuLadderService;
	
	@Autowired
	private MemberPriceService memberPriceService;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }
	
	@Override
	public void saveSkuReduction(SkuReductionTo reductionTo) {
		/*sku优惠满减等信息 gulimall_sms -> sms_sku_ladder\sms_sku_full_reduction\sms_member_price*/
		/*sms_sku_ladder*/
		SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
		skuLadderEntity.setSkuId(reductionTo.getSkuId());
		skuLadderEntity.setFullCount(reductionTo.getFullCount());
		skuLadderEntity.setDiscount(reductionTo.getDiscount());
		skuLadderEntity.setAddOther(reductionTo.getCountStatus());
		if (reductionTo.getFullCount() > 0) {
			skuLadderService.save(skuLadderEntity);
		}
		/*sms_sku_full_reduction*/
		SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
		BeanUtils.copyProperties(reductionTo, reductionEntity);
		if (reductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
			this.save(reductionEntity);
			
		}
		/*sms_member_price*/
		List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
		List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
			MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
			memberPriceEntity.setSkuId(reductionTo.getSkuId());
			memberPriceEntity.setMemberPrice(item.getPrice());
			memberPriceEntity.setMemberLevelName(item.getName());
			memberPriceEntity.setMemberLevelId(item.getId());
			memberPriceEntity.setAddOther(1);
			return memberPriceEntity;
		}).filter(item -> {
			return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
		}).collect(Collectors.toList());
		memberPriceService.saveBatch(collect);
		
	}
	
}